'use strict';

const { BaseParameter, stateKeys, addCommentSchema, CommentQuery } = require('../core/base_mongo_model');

module.exports = app => {
  const mongoose = app.mongoose;
  const Schema = mongoose.Schema;

  const commentQuery = CommentQuery(Schema)

  const productSchema = new Schema({
    ...BaseParameter,
    /**
     * 项目名称
     */
    name: { type: String, default: '' },
    /**
     * 项目图标
     */
    cover: { type: String, default: '' },
    /**
     * 版本
     */
    version: { type: String, default: '' },
    /**
     * 项目类型
     */
    type: { type: String, default: '' },
    /**
     * 内容
     */
    content: { type: String, default: '' },
    /**
     * 描述
     */
    describe: { type: String, default: '' },
    /**
     * 项目地址
     */
    url: { type: String, default: '' },
    /**
     * 项目开始时间
     */
    startDate: { type: Date },
    /**
     * 项目结束时间
     */
    endDate: { type: Date },
    /**
     * 是否在个人界面显示
     */
    isShow: { type: Boolean, default: false },
    // 评论
    ...commentQuery
  });

  // eslint-disable-next-line jsdoc/require-param
  /**
   * 获取所有类型
   */
  productSchema.statics.findTypes = function() {
    return this.distinct('type', {
      $or: [
        { stateKey: stateKeys.NORMAL },
        { stateKey: { $exists: false } },
      ],
    });
  };

  // eslint-disable-next-line jsdoc/require-param
  /**
   * 获取查询参数
   */
  productSchema.statics.getParmas = function(data) {
    const params = {
      $or: [
        { stateKey: stateKeys.NORMAL },
        { stateKey: { $exists: false } },
      ],
    };
    if (data.type !== 'all') {
      params.type = data.type;
    }
    if (data.searchKey) {
      const reg = { $regex: `${data.searchKey}`, $options: 'i' };
      params.name = reg;
    }
    return params;
  };

  // eslint-disable-next-line jsdoc/require-param
  /**
   * 获取列表数据
   */
  productSchema.statics.findList = function(data) {
    const params = this.getParmas(data);
    const skip = (data.pageNum - 1) * data.pageSize;
    const model = this.find(params, 'name cover type describe url startDate endDate isShow').skip(skip).limit(data.pageSize);
    model.sort({ date: -1 });
    return model.exec();
  };

  // eslint-disable-next-line jsdoc/require-param
  /**
   * 获取个数
   */
  productSchema.statics.findCount = function(data) {
    const params = this.getParmas(data);
    return this.countDocuments(params);
  };

  // eslint-disable-next-line jsdoc/require-param
  /**
   * 获取单个的详情信息
   */
  productSchema.statics.findDetail = function(id) {
    return this.findOne({
      _id: mongoose.Types.ObjectId(id),
    }).populate({
      path: 'commentsList',
      options: { sort: '-date' },
      populate: {
        path: 'children',
        options: { sort: '-date' },
      },
    });
  };

  // eslint-disable-next-line jsdoc/require-param
  /**
   * 更新数据
   */
  productSchema.statics.updateModel = function(id, upData) {
    return this.updateOne({
      _id: mongoose.Types.ObjectId(id),
    }, upData);
  };

  /**
   * 删除单个数据
   * @param id 要删除数据的ID
   * @param thoroughly 是否测地删除数据
   * @returns {*}
   */
  productSchema.statics.removeModel = function(id, thoroughly = false) {
    if (thoroughly) {
      return this.deleteOne({
        _id: mongoose.Types.ObjectId(id),
      });
    }
    return this.updateOne({
      _id: mongoose.Types.ObjectId(id),
    }, {
      stateKey: stateKeys.DUSTBIN,
    });
  };

  // eslint-disable-next-line jsdoc/require-param
  /**
   * 从回收站还原
   */
  productSchema.statics.dustbinRestore = function(id) {
    return this.updateOne({
      _id: mongoose.Types.ObjectId(id),
    }, {
      stateKey: stateKeys.NORMAL,
    });
  };

  addCommentSchema(productSchema, mongoose)

  return mongoose.model('Production', productSchema, 'productions');
};
